function [FullMS,axis_min,axis_mz] = cdf2mat(file,rate_MS)

% Date: 30/05/2022
%
% Please use this function to open MS-based chromatographic data from NETCDF files.
%
% Resampling included for non-integer acquisition rates. Outputs nominal
% mass.
%
% INPUT
% file:    Opens the netCDF like 'Sample01.CDF'
% rate_MS: Desired integer acquisition rate
%
% OUTPUT
% FullMS      Full MS chromatogram (second order data tensor)
% axis_min    Retention time axis in minutes
% axis_mz     m/z axis in Daltons
%
% I/O: [FullMS,axis_min,axis_mz] = cdf2mat(file,rate_MS)
%
% Compiled with MATLAB R2021b (9.11.0.1809720). Requires the Signal Processing Toolbox v.9.0.
% Based on netCDFload.m (Murphy, Wenig, Parcsi, Skov e Stuetz) e de iCDF_load (Skov e Bro 2008).
% K.R. Murphy, P. Wenig, G. Parcsi, T. Skov, R.M. Stuetz (in press) Characterizing odorous emissions 
% using new software for identifying peaks in chemometric models of GC-MS datasets. Chem Intel Lab Sys.
% doi: 10.1016/j.chemolab.2012.07.006
% Skov T and Bro R. (2008) Solving fundamental problems in chromatographic
% analysis, Analytical and Bioanalytical Chemistry, 390 (1): 281-285.
% doi: 10.1007/s00216-007-1618-z

Scan = ncread(file,'scan_index');
Valores_massa = ncread(file,'mass_values');
Intensidade = ncread(file,'intensity_values');
TR1 = ncread(file,'scan_acquisition_time');
Massa_max = ncread(file,'mass_range_max');

Matriz1 = zeros(length(Scan),max(round(Valores_massa)));
Scan(length(Scan)+1)= length(Valores_massa);
axis_mz = 1:round(max(Massa_max));

for i = 1:length(Scan)-1
    Matriz1(i,round(Valores_massa(Scan(i)+1:Scan(i+1))))= Intensidade(Scan(i)+1:Scan(i+1));
end

for j = 1:length(Matriz1(1,:))
    [FullMS(:,j),TR2] = resample(Matriz1(:,j),TR1(:,1),rate_MS);
end

FullMS = max(FullMS,0);

axis_min = TR2/60;


%END